import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

// 解决跳转同一路由的报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  {
    path:'/login',
    component:()=>import('../views/login/index.vue'),
  },
  {
    path:'/',
    component:()=>import('../views/main.vue'),
    redirect: '/home',
    children:[
      {
        path:'/home',
        component:()=>import('../views/Home/index.vue'),
        meta:{
          label:'首页',
          Icon:'el-icon-s-home',
          Grade:'*'
        },
      },
      {
        path:'/mall',
        component:()=>import('../views/mall/index.vue'),
        meta:{
          label:'商品管理',
          Icon:'el-icon-s-goods',
          Grade:['admin','editor']
        },
      },
      {
        path:'/user',
        component:()=>import('../views/user/index.vue'),
        redirect: '/user/edit',
        meta:{
          label:'用户管理',
          Icon:'el-icon-user-solid',
          Grade:['admin']
        },
        children:[
          {
            path:'edit',
            component:()=>import('../views/user/userEdit/index.vue'),
            meta:{
              label:'用户编辑',
              Icon:'el-icon-edit',
              Grade:['admin']
            },
          },
          {
            path:'loa',
            component:()=>import('../views/user/userRights/index.vue'),
            meta:{
              label:'用户权限',
              Icon:'el-icon-s-custom',
              Grade:['admin']
            },
          },
        ],
      },
      {
        path:'/page',
        component:()=>import('../views/page/index.vue'),
        redirect: '/page/',
        meta:{
          label:'其他',
          Icon:'el-icon-receiving',
          Grade:['admin','editor']
        },
        children:[
          {
            path:'page1',
            component:()=>import('../views/page/page1.vue'),
            meta:{
              label:'页面1',
              Icon:'el-icon-receiving',
              Grade:['admin','editor']
            },
          },
          {
            path:'page2',
            component:()=>import('../views/page/page2.vue'),
            meta:{
              label:'页面2',
              Icon:'el-icon-receiving',
              Grade:['admin','editor']
            },
          },
        ],
      },
    ],
  },
]

const router = new VueRouter({
  routes
})

//全局前置守卫，权限校验
router.beforeEach((to,from,next)=>{
  if(to.path === '/login') return next()
  if(store.state.islogin){
    const Grade = store.state.user.userifo.Grade
    const GradeConfig = to.meta.Grade
    if(!(GradeConfig !== '*' && typeof GradeConfig.length === 'number' && GradeConfig.indexOf(Grade) === -1)){
      next()
    }
  }else{
    next('/login')
  }
})

//全局后置守卫，用于更新VueX的当前路由路径对象和路由路径对象列表
router.afterEach((to,from)=>{
  if(to.path !== '/login'){
    const obj = {
      label: to.meta.label,
      path: to.path,
    }
    store.commit('ADDROUTERPAHT', obj)
  }
})

export default router
